package org.liaohailong.helloworld.study;

import java.util.Stack;

/**
 * Author: liaohailong
 * Time: 2021/5/10 17:24
 * Describe: 两个栈，实现一个队列
 */
public class MyQueue {
    Stack<Integer> front = new Stack<>();
    Stack<Integer> back = new Stack<>();

    /**
     * Initialize your data structure here.
     */
    public MyQueue() {

    }

    /**
     * Push element x to the back of queue.
     */
    public void push(int x) {
        front.push(x);
    }

    /**
     * Removes the element from in front of queue and returns that element.
     */
    public int pop() {
        back.clear();
        while (!front.isEmpty()) {
            Integer top = front.pop();
            back.push(top);
        }
        Integer pop = back.pop();
        while (!back.isEmpty()) {
            Integer top = back.pop();
            front.push(top);
        }
        return pop;
    }

    /**
     * Get the front element.
     */
    public int peek() {
        back.clear();
        while (!front.isEmpty()) {
            Integer top = front.pop();
            back.push(top);
        }
        Integer peek = back.peek();
        while (!back.isEmpty()) {
            Integer top = back.pop();
            front.push(top);
        }
        return peek;
    }

    /**
     * Returns whether the queue is empty.
     */
    public boolean empty() {
        return front.size() <= 0;
    }
}
